import { ajax } from 'nc-lightapp-front';
import { calcTotal } from '../../../../common/util/calcUtils';
import { REQUEST_URL, CARD } from '../../constant';
import { javaAfterHeadEvent} from './afterHeadEvent';
/**
* @description: 表体编辑后事件
* @param: moduleId 区域编码
* @param: key 当前字段编码
*/
export function afterTableEvent(props, moduleId, key, value, changedrows, index, record, type, method) {
    let meta = props.meta.getMeta();
    if ('def1' == key) {
        props.cardTable.setValByKeyAndIndex(CARD.table_code, index, 'def2', {value: '', display: ''});
        props.cardTable.setValByKeyAndIndex(CARD.table_code, index, 'def3', {value: '', display: ''});
        props.cardTable.setValByKeyAndIndex(CARD.table_code, index, 'def4', {value: '', display: ''});
        props.cardTable.setValByKeyAndIndex(CARD.table_code, index, 'def5', {value: '', display: ''});
        props.cardTable.setValByKeyAndIndex(CARD.table_code, index, 'def6', {value: '', display: ''});
        props.cardTable.setValByKeyAndIndex(CARD.table_code, index, 'def7', {value: '', display: ''});
        props.cardTable.setValByKeyAndIndex(CARD.table_code, index, 'def8', {value: '', display: ''});
        props.cardTable.setValByKeyAndIndex(CARD.table_code, index, 'def9', {value: '', display: ''});
        props.cardTable.setValByKeyAndIndex(CARD.table_code, index, 'def10', {value: '', display: ''});
        props.cardTable.setValByKeyAndIndex(CARD.table_code, index, 'def11', {value: '', display: ''});
        props.cardTable.setValByKeyAndIndex(CARD.table_code, index, 'def12', {value: '', display: ''});
    } else if ('def6' == key) {
        // let data = {
        //     editKey: key,
        //     data: value.value
        // }
        // javaBreditActionTableEvent(props, data);
    } else if ('acc' == key) {
        // let data = {
        //     editKey: key,
        //     data: [
        //         {
        //             key: key,
        //             value: value.value
        //         }
        //     ]
        // }
        // //法人会计科目
        // if (value.value == '') {
        //     meta[CARD.table_code] && meta[CARD.table_code].items.map((item) => {
        //         if (item.attrcode == 'def5' || item.attrcode == 'def7' || item.attrcode == 'def12' || item.attrcode == 'def11' || item.attrcode == 'def10') {
        //             item.required = false
        //         }
        //     })
        //     props.meta.setMeta(meta);
        // } else {
        //     ajax({
        //         url: REQUEST_URL.bankName,
        //         data: data,
        //         success: (res) => {
        //             //辅助核算对应的字段编码
        //             let a = res.data.value;
        //             meta[CARD.table_code] && meta[CARD.table_code].items.map((item) => {
        //                 if (item.attrcode == 'def5' || item.attrcode == 'def7' || item.attrcode == 'def12' || item.attrcode == 'def11' || item.attrcode == 'def10') { 
        //                     setRequired(item, a);
        //                 } 
        //             })
        //             props.meta.setMeta(meta);
        //         }
        //     })
        // }
    }else if ('money' == key) {
        // javaAfterHeadEvent(props, 'tax');
        let head_keys = ['tax'];
        let body_keys = ['money'];
        let value = null;
        value = calcTotal(props, CARD.form_id, head_keys, CARD.table_code, body_keys);
        props.form.setFormItemsValue(CARD.form_id, { 'count': { value: value} });
    }

    function javaTreditActionTableEvent(props, data) {
        if (value.value == '') {
            meta[CARD.table_code] && meta[CARD.table_code].items.map((item) => {
                if (item.attrcode == 'def6' || item.attrcode == 'def7' || item.attrcode == 'lease' || item.attrcode == 'billno') {
                    item.required = false
                }
            })
            props.meta.setMeta(meta);
        } else {
            ajax({
                url: REQUEST_URL.tableEven,
                data: data,
                success: (res) => {
                    let items = res.data;
                    meta[CARD.table_code] && meta[CARD.table_code].items.map((item) => {
                        if (item.attrcode == 'def6') {
                            if (items.key == '15') {
                                item.required = true
                            } else {
                                item.required = false
                            }
                        } else if (item.attrcode == 'def7') {
                            if (items.key == '02' || items.key == '38' || items.key == '33') {
                                item.required = true
                            } else {
                                item.required = false
                            }
                        } else if (item.attrcode == 'lease') {
                            if (items.key == '06') {
                                item.required = true
                            } else {
                                item.required = false
                            }
                        } else if (item.attrcode == 'billno') {
                            if (items.key == '38') {
                                item.required = true
                            } else {
                                item.required = false
                            }
                        }
                    })
                    props.meta.setMeta(meta);
                }
            })
        }
    }

    function javaBreditActionTableEvent(props, data) {
        if (value.value == '') {
            meta[CARD.table_code] && meta[CARD.table_code].items.map((item) => {
                if (item.attrcode == 'def9' || item.attrcode == 'def7') {
                    item.required = false
                }
            })
            props.meta.setMeta(meta);
        } else {
            ajax({
                url: REQUEST_URL.tableEven,
                data: data,
                success: (res) => {
                    debugger
                    let items = res.data.key;
                    meta[CARD.table_code] && meta[CARD.table_code].items.map((item) => {
                        if (items == 0) {
                            if (item.attrcode == 'def7') {
                                item.required = true
                            }
                        } else if (items == 3) {
                            if (item.attrcode == 'def9') {
                                item.required = true
                            }
                        }
                    })
                    props.meta.setMeta(meta);
                }
            })
        }
    }
}
